package com.hejian.android.a72ggametools.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.bean.AdvItem;
import com.hejian.android.a72ggametools.imageloader.DoubleCache;
import com.hejian.android.a72ggametools.imageloader.ImageCache;
import com.hejian.android.a72ggametools.imageloader.ImageLoader;
import com.hejian.android.a72ggametools.viewholder.ActionInfoViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2016/1/15.
 */
public class ActionAdapter extends BaseAdapter {
    private static final String TAG = "zzzz";
    private Context context;
    private List<AdvItem> advItemList;
    private ActionInfoViewHolder actionInfoViewHolder;
    private ImageLoader imageLoader;
    private ImageCache imageCache;

    public ActionAdapter(Context context, List<AdvItem> advItemList) {
        this.context = context;
        this.advItemList = advItemList;
        Log.d(TAG, "ActionAdapter: "+advItemList.size());
        imageCache = new DoubleCache();
        imageLoader = ImageLoader.getInstance(context,imageCache);
    }

    @Override
    public int getCount() {
        return advItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return advItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.action_listview_item, null);
            actionInfoViewHolder = new ActionInfoViewHolder();
            actionInfoViewHolder.imageView = (ImageView) view.findViewById(R.id.action_listview_item_img);
            actionInfoViewHolder.title = (TextView) view.findViewById(R.id.action_item_title);
            actionInfoViewHolder.content = (TextView) view.findViewById(R.id.action_item_content);
            actionInfoViewHolder.joiner = (TextView) view.findViewById(R.id.action_item_joiner);
            actionInfoViewHolder.status = (TextView) view.findViewById(R.id.action_item_status);
            view.setTag(actionInfoViewHolder);
        }else {
            actionInfoViewHolder = (ActionInfoViewHolder) view.getTag();
        }
        if (advItemList != null && advItemList.size() > 0){
            AdvItem advItem = advItemList.get(position);
            actionInfoViewHolder.title.setText(advItem.getaName());
            actionInfoViewHolder.content.setText(advItem.getShortName());
            actionInfoViewHolder.joiner.setText(advItem.getTotalJoinUser()+"人参加");
            actionInfoViewHolder.status.setText(advItem.getStatus());
            actionInfoViewHolder.imageView.setImageBitmap(null);
            imageLoader.displayImage(advItem.getHotPic(),actionInfoViewHolder.imageView);
        }
        return view;
    }
}
